package com.xy.modular.shop.action;

import com.xy.framework.base.controller.BaseController;
import com.xy.framework.mreturn.MReturnObject;
import com.xy.framework.mreturn.MReturnUntil;
import com.xy.framework.pager.PageRequest;
import com.xy.framework.util.uuid.UUIDUtils;
import com.xy.modular.shop.entity.ProductType;
import com.xy.modular.shop.service.IProductTypeService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author BCK
 * @since 2019-10-12
 */
@Controller
@RequestMapping("/shop/product-type")
public class ProductTypeController extends BaseController {
	static Logger log = LoggerFactory.getLogger(ProductTypeController.class);

	@Autowired
	private IProductTypeService productTypeService;

	@RequiresRoles("SYSTEMER")
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public @ResponseBody MReturnObject listModule(ProductType productType, HttpServletRequest request) {
		PageRequest pageRequest = newPageRequest(request);
		return new MReturnObject(MReturnObject.SUCCESS, productTypeService.findAll(pageRequest),
				productTypeService.getCount(pageRequest));
	}

	@RequiresRoles("SYSTEMER")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public @ResponseBody MReturnObject save(ProductType productType) {
		try {
			productTypeService.validateEntity(productType);
			productType.setId(UUIDUtils.UUID());
			productTypeService.save(productType);
		} catch (Exception e) {
			log.info("ProductType保存失败", e);
			return new MReturnObject(MReturnObject.SUCCESS, MReturnUntil.ERROR_MOUDLE_SAVE);
		}
		return new MReturnObject(MReturnObject.SUCCESS);
	}

	@RequiresRoles("SYSTEMER")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody MReturnObject update(ProductType productType) {
		try {
			productTypeService.validateEntity(productType);
			productTypeService.update(productType);
		} catch (Exception e) {
			log.info("ProductType修改失败", e);
			return new MReturnObject(MReturnObject.ERROR, MReturnUntil.ERROR_MOUDLE_SAVE);
		}
		return new MReturnObject(MReturnObject.SUCCESS);
	}

	@RequiresRoles("SYSTEMER")
	@RequestMapping(value = "/del/{id}", method = RequestMethod.GET)
	public @ResponseBody MReturnObject del(@PathVariable String id) {
		try {
			productTypeService.deleteById(id);
		} catch (Exception e) {
			log.info("ProductType删除失败", e);
			return new MReturnObject(MReturnObject.ERROR, MReturnUntil.ERROR_MOUDLE_SAVE);
		}
		return new MReturnObject(MReturnObject.SUCCESS);
	}
}
